<?php
namespace app\massage\controller;
use app\AdminRest;
use app\massage\model\AdminRole;
use app\massage\model\CarUserTrophy;
use app\massage\model\Commission;
use app\shop\model\Article;
use app\shop\model\Banner;
use app\shop\model\Date;
use app\massage\model\OrderGoods;
use app\massage\model\RefundOrder;
use app\shop\model\IntegralLog;
use app\shop\model\Wallet;
use think\App;
use app\massage\model\User as Model;


class AdminUser extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new Model();

        $this->order_goods_model = new OrderGoods();

        $this->refund_order_model = new RefundOrder();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-24 10:24
     * @功能说明:用户列表
     */
    public function userList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];
        //是否授权
        if(!empty($input['type'])){

            if($input['type']==1){

                $dis[] = ['nickName','=',''];

            }else{

                $dis[] = ['nickName','<>',''];

            }

        }

        if(isset($input['role'])){

            $dis[] = ['role','=',$input['role']];
        }

        $where = [];

        if(!empty($input['nickName'])){

            $where[] = ['nickName','like','%'.$input['nickName'].'%'];

            $where[] = ['phone','like','%'.$input['nickName'].'%'];
        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $start_time = $input['start_time'];

            $end_time = $input['end_time'];

            $dis[] = ['create_time','between',"$start_time,$end_time"];
        }

        $data = $this->model->dataList($dis,$input['limit'],$where);



        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-26 14:39
     * @功能说明:编辑员工
     */
    public function userUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
         ];

        $res = $this->model->dataUpdate($dis,$input);

        return $this->success($res);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-08-28 23:03
     * @功能说明:佣金记录
     */
    public function commList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];


        if(!empty($input['status'])){

            $dis[] = ['a.status','=',$input['status']];
        }

        if(!empty($input['top_name'])){

            $dis[] = ['c.nickName','like','%'.$input['top_name'].'%'];

        }

        $comm_model = new Commission();

        $data = $comm_model->recordList($dis,$input['limit']);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-26 15:02
     * @功能说明:用户所有获得的奖杯
     */
    public function userTrophy(){

        $input = $this->_param;

        $user_trophy_model = new CarUserTrophy();

        $dis = [

            'a.user_id' => $input['user_id'],

            'a.status'  => 1,

            'b.status'  => 1

        ];

        $data = $user_trophy_model->alias('a')
                ->join('shequshop_car_trophy b','a.trophy_id = b.id')
                ->where($dis)
                ->field('a.*,b.title,b.cover')
                ->select()
                ->toArray();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-26 15:04
     * @功能说明:用户添加奖杯
     */
    public function userTrophyAdd(){

        $input = $this->_input;

        $user_trophy_model = new CarUserTrophy();

        foreach ($input['user_id'] as $value){

            foreach ($input['trophy_id'] as $v){

                $dis = [

                    'user_id'  => $value,

                    'trophy_id'=> $v,

                    'uniacid'  => $this->_uniacid
                ];

                $find = $user_trophy_model->where($dis)->where('status','>',-1)->find();
                //增加
                if($input['add']==1){

                     if(empty($find)){

                         $user_trophy_model->dataAdd($dis);
                     }

                }else{
                    //减少
                    if(!empty($find)){

                        $user_trophy_model->dataUpdate($dis,['status'=>-1]);
                    }

                }

            }

        }

        return $this->success(true);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-07-20 16:00
     * @功能说明:修改用户积分
     */
    public function userIntegralUpdate(){

        $input = $this->_input;

        $log_model = new IntegralLog();

        $type = $input['integral']>0?12:13;

        $res = $log_model->integralUserAdd($input['user_id'],$input['integral'],$this->_uniacid,2,$type);

        return $this->success($res);

    }









}
